<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('出资人名称')" prop="investorName">
            <el-input v-model="form.investorName" :placeholder="$t('出资人名称')" show-word-limit maxlength="100" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目')" prop="assignItems">
            <el-input v-model="form.assignItems" :placeholder="$t('分配项目')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('分配总金额')+$t('金额单位')" prop="totalAmount">
            <Money v-model="form.totalAmount" :placeholder="$t('分配总金额')" unit="w" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-show="isRate" :label="$t('汇率')" prop="exchangeRate">
            <Money v-model="form.exchangeRate" :placeholder="$t('汇率')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-show="isRate" :label="$t('分配总金额')+$t('RMB')+$t('金额单位')" prop="totalAmountRmb">
            <Money v-model="form.totalAmountRmb" :placeholder="$t('分配总金额') + $t('RMB')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本金')+$t('金额单位')" prop="principalCost">
            <Money v-model="form.principalCost" :placeholder="$t('本金')" unit="w" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('Hurdle')+$t('金额单位')" prop="hurdleTheory">
            <Money v-model="form.hurdleTheory" :placeholder="$t('Hurdle')" unit="w" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('股息红利')+$t('金额单位')" prop="dividends">
            <Money v-model="form.dividends" :placeholder="$t('股息红利')" unit="w" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('追赶条款')+$t('金额单位')" prop="chaseTerms">
            <Money v-model="form.chaseTerms" :placeholder="$t('追赶条款')" unit="w" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('剩余分配LP')+$t('金额单位')" prop="surplusLp">
            <Money v-model="form.surplusLp" :placeholder="$t('剩余分配LP')" unit="w" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('剩余分配GP')+$t('金额单位')" prop="surplusGp">
            <Money v-model="form.surplusGp" :placeholder="$t('剩余分配GP')" unit="w" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('代扣税费')+$t('金额单位')" prop="taxeFee">
            <Money v-model="form.taxeFee" :placeholder="$t('代扣税费')" unit="w" @change="calculation" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="fund:fp" />
    </div>
  </div>
</template>

<script>
import { saveFundFpDetails, getFundFpDetails } from '@/api/fund/fp/fundFpDetails'

export default {
  data() {
    return {
      // 表单参数
      form: {
      },
      // 遮罩层
      loading: false,
      isRate: false,
      // 表单校验
      rules: {
        investorName: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
        totalAmount: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
        exchangeRate: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined) {
        this.isRate = data.isRate
        getFundFpDetails(data.id).then(response => {
          this.form = response.data
          this.calculation()
        })
      } else {
        this.reset()
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: undefined,
        fpId: undefined,
        investorId: undefined,
        investorName: undefined,
        assignItems: undefined,
        fpTime: undefined,
        principalCost: undefined,
        hurdleTheory: undefined,
        chaseTerms: undefined,
        balanceFp: undefined,
        taxeFee: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined,
        totalAmount: undefined,
        exchangeRate: undefined,
        totalAmountRmb: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      if ((this.form.exchangeRate === null || this.form.exchangeRate === undefined || this.form.exchangeRate === '') && !this.isRate) {
        this.form.exchangeRate = 1
      }
      if ((this.form.totalAmountRmb === null || this.form.totalAmountRmb === undefined || this.form.totalAmountRmb === '') && !this.isRate) {
        this.form.totalAmountRmb = this.form.totalAmount
      }
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundFpDetails(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 计算总金额
    calculation() {
      this.form.totalAmount =
          this.transZero(this.form.principalCost) +
          this.transZero(this.form.hurdleTheory) +
          this.transZero(this.form.dividends) +
          this.transZero(this.form.surplusLp) +
          this.transZero(this.form.surplusGp) +
          this.transZero(this.form.chaseTerms)
      if (!this.isRate) {
        this.form.exchangeRate = 1
      }
      this.form.totalAmountRmb = this.form.totalAmount * this.form.exchangeRate
    },
    transZero(val) {
      if (val !== undefined && val !== null && val !== '') {
        return val
      }
      return 0
    }

  }

}
</script>
